<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
class Product_model extends CI_Model {
	public function __construct() {
		parent::__construct ();
	}
	/*
	 * 得到上架的商品信息(用于导出商品)
	 */
	public function find_products(){
		$this->db->where("show", 1);
		$this->db->order_by("categoryid", "asc");
		$this->db->order_by("varietyid", "asc");		
		$query=$this->db->get('t_product');		
		return $query->result_array();
	}
	public function find_all_products($per_page,$offset){
		$this->db->select('t_product.id,t_category.name as category,t_product.varietyid,t_product.name,t_product.image,t_product.description,t_product.purprice,t_product.price,t_product.unit,t_product.position,t_product.show,t_product.discount');
		//$this->db->from('t_product');
		//$this->db->join('t_category', 't_category.id = t_product.categoryid','left');
		$this->db->from('t_product,t_category');
		$this->db->where('t_category.id = t_product.categoryid');
		$this->db->limit($per_page, $offset);
		$query = $this->db->get();
		return $query->result_array();
	}
	/*
	 * 得到库存相关商品信息
	 */
	public function find_stock_products(){
		$this->db->select('t_product.id,t_product.name,t_product.purprice,t_product.price,t_product.unit,t_product.description,t_product.short,t_product.isstock,t_product.stock');
		$this->db->from('t_product');
		$this->db->where('show',1);
		$this->db->where('categoryid in(16,18,20,21,24)');
		$this->db->order_by("categoryid", "asc");
		$this->db->order_by("varietyid", "asc");			
		$query = $this->db->get();
		return $query->result_array();
	}
	/*
	 * 得到所有上架的库存商品信息
	 */
	public function find_all_stock_products(){
		$this->db->where('show',1);
		$this->db->where('isstock',1);
		$this->db->order_by("stock", "asc");
		$query = $this->db->get('t_product');
		return $query->result_array();
	}
	/*
	 * 得到库存商品数量
	 */
	public function count_stock_products($name){
		$this->db->where('isstock',1);
		$this->db->like('name', $name);		
		$query = $this->db->get('t_product');
		return $query->num_rows();
	}
	/*
	 * 根据分页信息得到库存商品信息
	 */
	public function find_stock_products_by_name($per_page,$offset,$name){
		$this->db->where('isstock',1);
		$this->db->like('name', $name);	
		$this->db->limit($per_page, $offset);
		$this->db->order_by("id", "desc");
		$query = $this->db->get('t_product');
		return $query->result_array();
	}
	/*
	 * 得到补货中商品数量
	 */
	public function count_short_products(){
		$this->db->where('short',1);	
		$query = $this->db->get('t_product');
		return $query->num_rows();
	}
	/*
	 * 根据分页信息得到补货中商品信息
	 */
	public function find_short_products($per_page,$offset){
		$this->db->where('short',1);	
		$this->db->limit($per_page, $offset);
		$query = $this->db->get('t_product');
		return $query->result_array();
	}
	/*
	 * 根据商品ID得到商品信息
	 */
	public function load($id){
		$this->db->select('t_product.id,t_product.categoryid,t_category.name as category,t_product.varietyid,t_product.name,t_product.image,t_product.description,t_product.purprice,t_product.price,t_product.unit,t_product.position,t_product.isstock,t_product.stock,t_product.sale,t_product.type,t_product.divide');
		//$this->db->from('t_product');
		$this->db->from('t_product,t_category,t_variety');
		$this->db->where('t_category.id = t_product.categoryid');
		$this->db->where('t_product.id',$id);
		//$this->db->join('t_category', 't_category.id = t_product.categoryid','left');
		$query = $this->db->get();
		if ($row = $query->row_array()){
			return $row;
		}
		return array();
	}
	/*
	 * 添加商品信息
	 */
	public function create($product){
		$this->db->insert('t_product', $product);
		return $this->db->insert_id();
	}
	/*
	 * 更新商品信息
	 */
	public function update($id,$product){
		$this->db->where('id', $id);
		$this->db->update('t_product', $product);
		return $this->db->affected_rows()>0;
	}
	/*
	 * 删除商品信息
	 */
	public function delete($id){
		$this->db->where('id', $id);
		$this->db->delete('t_product');
		return $this->db->affected_rows()>0;
	}
	/*
	 * 根据小类ID查询该小类下是否有商品
	 */
	public function check_product_by_var($varietyid){
		$this->db->select('id');
		$query = $this->db->get_where('t_product',array('varietyid' => $varietyid));
		return $query->row_array();
	}
	/*
	 * 根据商品名称，类别，是否特价，类型，上下架来得到对应的商品的数量
	 */
	public function count_products($name,$category,$variety,$sale,$purprice,$type,$show){
		if($category!=0){
			$this->db->where('categoryid',$category);
		}
		if($variety!=0){
			$this->db->where('varietyid',$variety);
		}
		if($purprice==1){
			$this->db->where('purprice=0');
		}
		if($purprice==2){
			$this->db->where('purprice!=0');
		}
		if($show==1){
			$this->db->where('show=1');
		}
		if($show==2){
			$this->db->where('show=0');
		}
		$this->db->where('sale',$sale);
		$this->db->where('type',$type);
		$this->db->like('name', $name);
		$query = $this->db->get('t_product');
		return $query->num_rows();
	}
	/*
	 * 根据商品名称，类别，是否特价，类型，上下架来得到对应的商品
	 */
	public function search_products_by_name_and_cat($per_page,$offset,$name,$type,$order,$category,$variety,$sale,$purprice,$type2,$show){
		$this->db->select('t_product.id,t_category.name as category,t_product.varietyid,t_product.name,t_product.image,t_product.description,t_product.purprice,t_product.price,t_product.unit,t_product.position,t_product.show,t_product.discount,t_product.sale,t_product.export,t_product.start,t_product.type');
		$this->db->from('t_product,t_category');
		$this->db->where('t_category.id = t_product.categoryid');
		if($category!=0){
			$this->db->where('t_product.categoryid',$category);
		}
		if($variety!=0){
			$this->db->where('t_product.varietyid',$variety);
		}
		if($purprice==1){
			$this->db->where('t_product.purprice=0');
		}
		if($purprice==2){
			$this->db->where('t_product.purprice!=0');
		}
		if($show==1){
			$this->db->where('show=1');
		}
		if($show==2){
			$this->db->where('show=0');
		}
		$this->db->where('t_product.sale',$sale);
		$this->db->where('t_product.type',$type2);
		$this->db->like('t_product.name', $name);
		$this->db->limit($per_page, $offset);
		$this->db->order_by($type, $order);
		$query = $this->db->get();
		return $query->result_array();
	}
	/*
	 * 根据商品名称返回商品信息
	 */
	public function get_products_by_name($name){
		$this->db->select('id,name,description,price,purprice,unit');
		$this->db->like('name', $name);
		$query=$this->db->get('t_product');
		return $query->result_array();
	}
	/*
	 * 根据商品名称返回上架商品的信息
	 */
	public function get_show_product_by_name($name){
		$this->db->select('id,name,description,price,purprice,unit');
		$this->db->where("show", 1);
		$this->db->like('name', $name);
		$query=$this->db->get('t_product');
		return $query->result_array();
	}
	/*
	 * 根据商品名称和用户类型返回上架商品的信息
	 */
	public function get_show_products_by_name($name,$type){
		$this->db->select('id,name,description,price,purprice,unit');
		if($type!=1){
			$this->db->where('type!=1');
		}
		if($type!=2){
			$this->db->where('type!=2');
		}
		$this->db->where("show", 1);
		$this->db->like('name', $name);
		$query=$this->db->get('t_product');
		return $query->result_array();
	}
}